package com.fastbee.web.controller.bigScreen;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.domain.R;
import com.fastbee.iot.model.CommonStatisVo;
import com.fastbee.iot.model.EnergyConsumptionVo;
import com.fastbee.iot.model.EnergyFlowMonitorVo;
import com.fastbee.iot.model.RealEnergyMonitiorVo;
import com.fastbee.iot.service.IAlertLogService;
import com.fastbee.iot.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fastbee.iot.tdengine.service.ILogService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "能耗")
@RestController
@RequestMapping("/bigScreen/energy")
public class EnergyController {
    private static final Logger log = LoggerFactory.getLogger(EnergyController.class);

    @Resource
    private ILogService logService;

    @Autowired
    private IAlertLogService alertLogService;

    @Autowired
    private IDeviceService deviceService;

    private static String allNumberList = "DB156ACD45A4B1,DB256ACD45A4C1,BCFF4D1580F1,308398971ABC,308398968084,BCFF4D14B712,BCFF4D146668,BCFF4D146874,308398969DFF,BCFF4D14012B,BCFF4D1480CF,98CDAC1034C5,98CDAC1068A2,BCFF4D150D66,BCFF4D022023,BCFF4D147C17,BCFF4D147D51,3083989694D2";
    private static String oneNumberList = "DB156ACD45A4B1";
    private static String twoNumberList = "DB256ACD45A4C1";
    private static String thrNumberList = "BCFF4D1580F1,308398971ABC,308398968084,BCFF4D14B712,BCFF4D146668,BCFF4D146874,308398969DFF,BCFF4D14012B,BCFF4D1480CF,98CDAC1034C5,98CDAC1068A2,BCFF4D150D66,BCFF4D022023,BCFF4D147C17,BCFF4D147D51,3083989694D2";

    @GetMapping("/data/check")
    @ApiOperation("数据服务检查接口")
    public Integer dataCheck(){
        try {
            Long count = logService.monitiorDataCheck();
            if(count > 0){
                return 1;
            }
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @GetMapping("/consumption")
    @ApiOperation("首页-园区能耗统计")
    public R<List<CommonStatisVo>> selectEnergyConsumptionByMonthList(){
        String serialNumberStr = allNumberList;
        List<String> serialNumbers = new ArrayList<>();
        if(StringUtils.isNotEmpty(serialNumberStr)){
            serialNumbers = Arrays.asList(serialNumberStr.split(","));
        }

        RealEnergyMonitiorVo realEnergyMonitiorVo = logService.selectRealEnergyMonitior(1,serialNumbers,null);
        RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(1,serialNumbers,null);

        realEnergyMonitiorVo.setAnnualTotal(realEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
        realEnergyMonitiorVo.setMonthTotal(realEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
        realEnergyMonitiorVo.setTodayTotal(realEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

        Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
        for (CommonStatisVo statisVo : realEnergyMonitiorVo.getCommonStatisVoList()) {
            if(eleMap.get(statisVo.getName()) != null){
                statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
            }
        }

        //原始数据
//        List<CommonStatisVo> energyConsumptionVos = logService.selectEnergyConsumptionByMonthList(serialNumbers);
        return R.ok(realEnergyMonitiorVo.getCommonStatisVoList());
    }

    /**
     * 能耗分析-同环比分析
     * @param addressId 设备所属地址ID（1：1楼，2：2楼，3：12楼）
     * @param typeId 1:年，2：月，3：日
     * @return
     */
    @GetMapping("/selectCompareMonitior")
    @ApiOperation("能耗分析-同环比分析")
    public R<List<CommonStatisVo>> selectCompareMonitior(@RequestParam(value = "addressId",required = false) Integer addressId
            ,@RequestParam(value = "typeId") Integer typeId){
        String serialNumberStr = "";
        if(addressId == null){
            serialNumberStr = allNumberList;
        }else{
            if(addressId == 1){
                serialNumberStr = oneNumberList;
            }else  if(addressId == 2){
                serialNumberStr = twoNumberList;
            }else{
                serialNumberStr = thrNumberList;
            }
        }
        List<String> serialNumbers = new ArrayList<>();
        if(StringUtils.isNotEmpty(serialNumberStr)){
            serialNumbers = Arrays.asList(serialNumberStr.split(","));
        }

        Date now = new Date();
        Date lastDate = null;
        if(typeId == 1){
            lastDate = DateUtil.offsetMonth(now,-12);
        }else if(typeId == 2){
            lastDate = DateUtil.offsetMonth(now,-1);
        }else{
            lastDate = DateUtil.offsetMonth(now,-84);
        }
        lastDate = DateUtil.offsetHour(lastDate,16);
        now = DateUtil.offsetHour(now,16);
        //上一区间值
        RealEnergyMonitiorVo lastRealEnergyMonitiorVo = new RealEnergyMonitiorVo();
        if(typeId == 4){
            lastRealEnergyMonitiorVo = logService.selectRealEnergyMonitiorFour(typeId,serialNumbers,lastDate);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEleFour(typeId,serialNumbers,lastDate);

            lastRealEnergyMonitiorVo.setAnnualTotal(lastRealEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            lastRealEnergyMonitiorVo.setMonthTotal(lastRealEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            lastRealEnergyMonitiorVo.setTodayTotal(lastRealEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : lastRealEnergyMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }else{
            lastRealEnergyMonitiorVo = logService.selectRealEnergyMonitior(typeId,serialNumbers,lastDate);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,lastDate);

            lastRealEnergyMonitiorVo.setAnnualTotal(lastRealEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            lastRealEnergyMonitiorVo.setMonthTotal(lastRealEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            lastRealEnergyMonitiorVo.setTodayTotal(lastRealEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : lastRealEnergyMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }
        //当前区间值
        RealEnergyMonitiorVo realEnergyMonitiorVo = new RealEnergyMonitiorVo();
        if(typeId == 4){
            realEnergyMonitiorVo = logService.selectRealEnergyMonitiorFour(typeId,serialNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEleFour(typeId,serialNumbers,now);

            realEnergyMonitiorVo.setAnnualTotal(realEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            realEnergyMonitiorVo.setMonthTotal(realEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            realEnergyMonitiorVo.setTodayTotal(realEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : realEnergyMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }else{
            realEnergyMonitiorVo = logService.selectRealEnergyMonitior(typeId,serialNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,now);

            realEnergyMonitiorVo.setAnnualTotal(realEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            realEnergyMonitiorVo.setMonthTotal(realEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            realEnergyMonitiorVo.setTodayTotal(realEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : realEnergyMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }
        List<CommonStatisVo> lastList = lastRealEnergyMonitiorVo.getCommonStatisVoList();
        Map<String,List<CommonStatisVo>> lastMap = new HashMap<>();
        if(lastList.size() > 0){
            lastMap = lastList.stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
        }
        List<CommonStatisVo> nowList = realEnergyMonitiorVo.getCommonStatisVoList();

        for (int i = 0; i < nowList.size(); i++) {
            CommonStatisVo vo = nowList.get(i);
            if(typeId == 1){
                if(vo.getValueSort() > LocalDate.now().getMonthValue()){
                    continue;
                }
            }
            if(typeId == 2){
                if(vo.getValueSort() > LocalDate.now().getDayOfMonth()){
                    continue;
                }
            }
            if(i == 0){
                //第一个
                if(lastList.size() == nowList.size()){
                    //环比（上一个值）
                    vo.setLastBigValue(lastList.get(lastList.size() - 1).getValue());
                    //同比（上一区间）
                    vo.setLastValue(lastList.get(i).getValue());
                }else{
                    vo.setLastBigValue(BigDecimal.ZERO);
                    vo.setLastValue(BigDecimal.ZERO);
                }
                continue;
            }
            //环比（上一个值）
            vo.setLastBigValue(nowList.get(i - 1).getValue());
            if(lastList.size() == nowList.size()){
                //同比（上一区间）
                vo.setLastValue(lastList.get(i).getValue());
            }else{
                vo.setLastValue(BigDecimal.ZERO);
            }
        }
        for (CommonStatisVo statisVo : nowList) {
            if(typeId == 1){
                if(statisVo.getValueSort() > LocalDate.now().getMonthValue()){
                    continue;
                }
            }
            if(typeId == 2){
                if(statisVo.getValueSort() > LocalDate.now().getDayOfMonth()){
                    continue;
                }
            }
            BigDecimal value = statisVo.getValue();
            BigDecimal lastValue = statisVo.getLastValue();
            BigDecimal lastBigValue = statisVo.getLastBigValue();
            BigDecimal zero = BigDecimal.ZERO;
            //环比率
            if(lastBigValue.compareTo(zero) == 0){
                if(value.compareTo(zero) > 0){
                    statisVo.setLastBigValueRate(new BigDecimal("1"));
                }else{
                    statisVo.setLastBigValueRate(zero);
                }
            }else{
                statisVo.setLastBigValueRate(value.subtract(lastBigValue).divide(lastBigValue,2,RoundingMode.HALF_UP));
            }

            //同比率
            if(lastValue.compareTo(zero) == 0){
                if(value.compareTo(zero) > 0){
                    statisVo.setLastValueRate(new BigDecimal("1"));
                }else{
                    statisVo.setLastValueRate(zero);
                }
            }else{
                statisVo.setLastValueRate(value.subtract(lastValue).divide(lastValue,2,RoundingMode.HALF_UP));
            }
        }
        
        return R.ok(nowList);
    }

    /**
     * 能耗分析-综合排名
     * @param typeId 1:年，2：月，3：日
     * @return
     */
    @GetMapping("/selectEnergyRange")
    @ApiOperation("能耗分析-综合排名")
    public R<List<CommonStatisVo>> selectEnergyRange(@RequestParam(value = "typeId") Integer typeId){
        List<CommonStatisVo> resultList = new LinkedList<>();
        Date now = DateUtil.offsetHour(new Date(),16);
        //1楼
        CommonStatisVo vo1 = new CommonStatisVo();
        vo1.setName("一楼营业厅");
        List<String> oneNumbers = Arrays.asList(oneNumberList.split(","));
        RealEnergyMonitiorVo oneMonitiorVo = new RealEnergyMonitiorVo();
        if(typeId == 4){
            oneMonitiorVo = logService.selectRealEnergyMonitiorFour(typeId,oneNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEleFour(typeId,oneNumbers,now);

            oneMonitiorVo.setAnnualTotal(oneMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            oneMonitiorVo.setMonthTotal(oneMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            oneMonitiorVo.setTodayTotal(oneMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : oneMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }else{
            oneMonitiorVo = logService.selectRealEnergyMonitior(typeId,oneNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,oneNumbers,now);

            oneMonitiorVo.setAnnualTotal(oneMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            oneMonitiorVo.setMonthTotal(oneMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            oneMonitiorVo.setTodayTotal(oneMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : oneMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }
        BigDecimal oneValue = (typeId == 1 ? oneMonitiorVo.getAnnualTotal() : typeId == 2 ? oneMonitiorVo.getMonthTotal() : oneMonitiorVo.getTodayTotal());
        vo1.setValue(oneValue);

        //2楼
        CommonStatisVo vo2 = new CommonStatisVo();
        vo2.setName("二楼营业厅");
        List<String> twoNumbers = Arrays.asList(twoNumberList.split(","));
        RealEnergyMonitiorVo twoMonitiorVo = new RealEnergyMonitiorVo();
        if(typeId == 4){
            twoMonitiorVo = logService.selectRealEnergyMonitiorFour(typeId,twoNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEleFour(typeId,twoNumbers,now);

            twoMonitiorVo.setAnnualTotal(twoMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            twoMonitiorVo.setMonthTotal(twoMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            twoMonitiorVo.setTodayTotal(twoMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : twoMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }else{
            twoMonitiorVo = logService.selectRealEnergyMonitior(typeId,twoNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,twoNumbers,now);

            twoMonitiorVo.setAnnualTotal(twoMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            twoMonitiorVo.setMonthTotal(twoMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            twoMonitiorVo.setTodayTotal(twoMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : twoMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }
        BigDecimal twoValue = (typeId == 1 ? twoMonitiorVo.getAnnualTotal() : typeId == 2 ? twoMonitiorVo.getMonthTotal() : twoMonitiorVo.getTodayTotal());
        vo2.setValue(twoValue);

        //12楼
        CommonStatisVo vo3 = new CommonStatisVo();
        vo3.setName("12楼办公室");
        List<String> thrNumbers = Arrays.asList(thrNumberList.split(","));
        RealEnergyMonitiorVo thrMonitiorVo = new RealEnergyMonitiorVo();
        if(typeId == 4){
            thrMonitiorVo = logService.selectRealEnergyMonitiorFour(typeId,thrNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEleFour(typeId,thrNumbers,now);

            thrMonitiorVo.setAnnualTotal(thrMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            thrMonitiorVo.setMonthTotal(thrMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            thrMonitiorVo.setTodayTotal(thrMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : thrMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }else{
            thrMonitiorVo = logService.selectRealEnergyMonitior(typeId,thrNumbers,now);
            RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,thrNumbers,now);

            thrMonitiorVo.setAnnualTotal(thrMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
            thrMonitiorVo.setMonthTotal(thrMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
            thrMonitiorVo.setTodayTotal(thrMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

            Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
            for (CommonStatisVo statisVo : thrMonitiorVo.getCommonStatisVoList()) {
                if(eleMap.get(statisVo.getName()) != null){
                    statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()).setScale(2,RoundingMode.HALF_UP));
                }
            }
        }
        BigDecimal value = (typeId == 1 ? thrMonitiorVo.getAnnualTotal() : typeId == 2 ? thrMonitiorVo.getMonthTotal() : thrMonitiorVo.getTodayTotal());
        vo3.setValue(value);
        resultList.add(vo1);
        resultList.add(vo2);
        resultList.add(vo3);
        Collections.sort(resultList, (p1, p2) -> p2.getValue().compareTo(p1.getValue()));
        return R.ok(resultList);
    }

    /**
     * 能耗分析-预警分析
     * @param addressId 设备所属地址ID（1：1楼，2：2楼，3：12楼）
     * @param typeId 1:年，2：月，3：日
     * @return
     */
    @GetMapping("/selectRealEnergyAlarmMonitior")
    @ApiOperation("能耗分析-预警分析")
    public R<List<CommonStatisVo>> selectRealEnergyAlarmMonitior(@RequestParam(value = "addressId",required = false) Integer addressId
            ,@RequestParam(value = "typeId") Integer typeId){
        String serialNumberStr = deviceService.selectSerialNumbersByAddressId(addressId);
        List<String> serialNumbers = new ArrayList<>();
        if(StringUtils.isNotEmpty(serialNumberStr)){
            serialNumbers = Arrays.asList(serialNumberStr.split(","));
        }
        List<CommonStatisVo> realEnergyMonitiorVo = alertLogService.selectRealEnergyAlarmMonitior(typeId,serialNumbers,new Date());
        return R.ok(realEnergyMonitiorVo);
    }

    @GetMapping("/testDate")
    public R testDate(){
        JSONObject json = new JSONObject();
        json.put("date",new Date());
        json.put("date1",DateUtil.offsetHour(new Date(),8));
        json.put("date2",DateUtil.offsetHour(new Date(),16));
        return R.ok(json);
    }

    /**
     * 获取能流图能耗数据
     * @param typeId 1:年，2：月，3：日
     * @return
     */
    @GetMapping("/selectRealEnergyMonitiorFlow")
    @ApiOperation("获取能流图能耗数据")
    public R selectRealEnergyMonitiorFlow(@RequestParam(value = "typeId") Integer typeId){
        Date date = DateUtil.offsetHour(new Date(),16);
        Map<String,BigDecimal> resultMap = new HashMap<>();
        List<String> serialNumbers = new ArrayList<>();
        //一楼营业厅
        serialNumbers.add("DB156ACD45A4B1");
        RealEnergyMonitiorVo one = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("一楼营业厅",typeId == 1 ? one.getAnnualTotal() : (typeId == 2 ? one.getMonthTotal() : one.getTodayTotal()));
        //一楼营业厅_灯光
        serialNumbers.clear();
        serialNumbers.add("DB156ACD45A4B2");
        RealEnergyMonitiorVo onedg = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("一楼营业厅_灯光",typeId == 1 ? onedg.getAnnualTotal() : (typeId == 2 ? onedg.getMonthTotal() : onedg.getTodayTotal()));
        //一楼营业厅_空调
        serialNumbers.clear();
        serialNumbers.add("DB156ACD45A4B3");
        RealEnergyMonitiorVo onekt = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("一楼营业厅_空调",typeId == 1 ? onekt.getAnnualTotal() : (typeId == 2 ? onekt.getMonthTotal() : onekt.getTodayTotal()));
        //一楼营业厅_其他用电
        resultMap.put("一楼营业厅_其他用电",resultMap.get("一楼营业厅").subtract(resultMap.get("一楼营业厅_灯光")).subtract(resultMap.get("一楼营业厅_空调")));

        //二楼营业厅_灯光_大厅
        serialNumbers.clear();
        serialNumbers.add("DG58CD696A4D1");
        serialNumbers.add("DG58CD696A4D2");
        serialNumbers.add("DG58CD696A4D3");
        serialNumbers.add("DG58CD696A4D4");
        serialNumbers.add("DG58CD696A4D5");
        serialNumbers.add("DG58CD696A4D6");
        serialNumbers.add("DG58CD696A4D7");
        RealEnergyMonitiorVo twodgdt = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("二楼营业厅_灯光_大厅",typeId == 1 ? twodgdt.getAnnualTotal() : (typeId == 2 ? twodgdt.getMonthTotal() : twodgdt.getTodayTotal()));
        //二楼营业厅_灯光_办公室
        serialNumbers.clear();
        serialNumbers.add("BCFF4D13D109");
        serialNumbers.add("BCFF4D14BE51");
        serialNumbers.add("BCFF4D159F82");
        RealEnergyMonitiorVo twodgbgs = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("二楼营业厅_灯光_办公室",typeId == 1 ? twodgbgs.getAnnualTotal() : (typeId == 2 ? twodgbgs.getMonthTotal() : twodgbgs.getTodayTotal()));
        //二楼营业厅_灯光
        resultMap.put("二楼营业厅_灯光",resultMap.get("二楼营业厅_灯光_大厅").add(resultMap.get("二楼营业厅_灯光_办公室")));
        //二楼营业厅_空调_大厅
        serialNumbers.clear();
        serialNumbers.add("KT558CD66A4K2");
        serialNumbers.add("KT558CD66A4K3");
        serialNumbers.add("KT558CD66A4K4");
        serialNumbers.add("KT558CD66A4K5");
        RealEnergyMonitiorVo twoktdt = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("二楼营业厅_空调_大厅",typeId == 1 ? twoktdt.getAnnualTotal() : (typeId == 2 ? twoktdt.getMonthTotal() : twoktdt.getTodayTotal()));
        //二楼营业厅_空调_办公室
        serialNumbers.clear();
        serialNumbers.add("KT558CD66A4K1");
        RealEnergyMonitiorVo twoktbgs = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("二楼营业厅_空调_办公室",typeId == 1 ? twoktbgs.getAnnualTotal() : (typeId == 2 ? twoktbgs.getMonthTotal() : twoktbgs.getTodayTotal()));
        //二楼营业厅_空调
        resultMap.put("二楼营业厅_空调",resultMap.get("二楼营业厅_空调_办公室").add(resultMap.get("二楼营业厅_空调_大厅")));
        //二楼营业厅
        serialNumbers.clear();
        serialNumbers.add("DB256ACD45A4C1");
        RealEnergyMonitiorVo two = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        resultMap.put("二楼营业厅",typeId == 1 ? two.getAnnualTotal() : (typeId == 2 ? two.getMonthTotal() : two.getTodayTotal()));
        //二楼营业厅_其他用电
        resultMap.put("二楼营业厅_其他用电",resultMap.get("二楼营业厅").subtract(resultMap.get("二楼营业厅_空调")).subtract(resultMap.get("二楼营业厅_灯光")));

        //12楼办公室_1201办公室_灯光
        serialNumbers.clear();
        serialNumbers.add("BCFF4D1580F1");
        RealEnergyMonitiorVo thr1201dg = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1201办公室_灯光",typeId == 1 ? thr1201dg.getAnnualTotal() : (typeId == 2 ? thr1201dg.getMonthTotal() : thr1201dg.getTodayTotal()));
        //12楼办公室_1201办公室_办公用电
        serialNumbers.clear();
        serialNumbers.add("308398971ABC");
        serialNumbers.add("308398968084");
        RealEnergyMonitiorVo thr1201bgyd = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1201办公室_办公用电",typeId == 1 ? thr1201bgyd.getAnnualTotal() : (typeId == 2 ? thr1201bgyd.getMonthTotal() : thr1201bgyd.getTodayTotal()));
        //12楼办公室_1201办公室
        resultMap.put("12楼办公室_1201办公室",resultMap.get("12楼办公室_1201办公室_灯光").add(resultMap.get("12楼办公室_1201办公室_办公用电")));

        //12楼办公室_1202办公室_灯光
        serialNumbers.clear();
        serialNumbers.add("BCFF4D14B712");
        RealEnergyMonitiorVo thr1202dg = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1202办公室_灯光",typeId == 1 ? thr1202dg.getAnnualTotal() : (typeId == 2 ? thr1202dg.getMonthTotal() : thr1202dg.getTodayTotal()));
        //12楼办公室_1202办公室_办公用电
        serialNumbers.clear();
        serialNumbers.add("BCFF4D146668");
        serialNumbers.add("BCFF4D146874");
        RealEnergyMonitiorVo thr1202bgyd = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1202办公室_办公用电",typeId == 1 ? thr1202bgyd.getAnnualTotal() : (typeId == 2 ? thr1202bgyd.getMonthTotal() : thr1202bgyd.getTodayTotal()));
        //12楼办公室_1202办公室
        resultMap.put("12楼办公室_1202办公室",resultMap.get("12楼办公室_1202办公室_办公用电").add(resultMap.get("12楼办公室_1202办公室_灯光")));

        //12楼办公室_1203办公室_灯光
        serialNumbers.clear();
        serialNumbers.add("308398969DFF");
        RealEnergyMonitiorVo thr1203dg = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1203办公室_灯光",typeId == 1 ? thr1203dg.getAnnualTotal() : (typeId == 2 ? thr1203dg.getMonthTotal() : thr1203dg.getTodayTotal()));
        //12楼办公室_1203办公室_办公用电
        serialNumbers.clear();
        serialNumbers.add("BCFF4D14012B");
        serialNumbers.add("BCFF4D1480CF");
        RealEnergyMonitiorVo thr1203bgyd = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1203办公室_办公用电",typeId == 1 ? thr1203bgyd.getAnnualTotal() : (typeId == 2 ? thr1203bgyd.getMonthTotal() : thr1203bgyd.getTodayTotal()));
        //12楼办公室_1203办公室
        resultMap.put("12楼办公室_1203办公室",resultMap.get("12楼办公室_1203办公室_灯光").add(resultMap.get("12楼办公室_1203办公室_办公用电")));

        //12楼办公室_1204办公室_灯光
        serialNumbers.clear();
        serialNumbers.add("98CDAC1034C5");
        serialNumbers.add("98CDAC1068A2");
        RealEnergyMonitiorVo thr1204dg = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1204办公室_灯光",typeId == 1 ? thr1204dg.getAnnualTotal() : (typeId == 2 ? thr1204dg.getMonthTotal() : thr1204dg.getTodayTotal()));
        //12楼办公室_1204办公室_办公用电
        serialNumbers.clear();
        serialNumbers.add("BCFF4D147D51");
        serialNumbers.add("BCFF4D150D66");
        serialNumbers.add("BCFF4D022023");
        serialNumbers.add("BCFF4D147C17");
        serialNumbers.add("3083989694D2");
        RealEnergyMonitiorVo thr1204bgyd = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);
        resultMap.put("12楼办公室_1204办公室_办公用电",typeId == 1 ? thr1204bgyd.getAnnualTotal() : (typeId == 2 ? thr1204bgyd.getMonthTotal() : thr1204bgyd.getTodayTotal()));
        //12楼办公室_1204办公室
        resultMap.put("12楼办公室_1204办公室",resultMap.get("12楼办公室_1204办公室_办公用电").add(resultMap.get("12楼办公室_1204办公室_灯光")));

        //12楼办公室
        resultMap.put("12楼办公室",resultMap.get("12楼办公室_1204办公室").add(resultMap.get("12楼办公室_1203办公室")).add(resultMap.get("12楼办公室_1202办公室")).add(resultMap.get("12楼办公室_1201办公室")));
        //总能耗
        resultMap.put("总能耗",resultMap.get("12楼办公室").add(resultMap.get("二楼营业厅")).add(resultMap.get("一楼营业厅")));

        return R.ok(resultMap);
    }

    /**
     * 能耗实时监控
     * @param addressId 设备所属地址ID（1：1楼，2：2楼，3：12楼）
     * @param typeId 1:年，2：月，3：日
     * @return
     */
    @GetMapping("/selectRealEnergyMonitior")
    @ApiOperation("能耗实时监控")
    public R<RealEnergyMonitiorVo> selectRealEnergyMonitior(@RequestParam(value = "addressId",required = false) Integer addressId
            ,@RequestParam(value = "typeId") Integer typeId){
        String serialNumberStr = "";
        if(addressId == null){
            serialNumberStr = allNumberList;
        }else{
            if(addressId == 1){
                serialNumberStr = oneNumberList;
            }else  if(addressId == 2){
                serialNumberStr = twoNumberList;
            }else{
                serialNumberStr = thrNumberList;
            }
        }
        Date date = DateUtil.offsetHour(new Date(),16);
        List<String> serialNumbers = new ArrayList<>();
        if(StringUtils.isNotEmpty(serialNumberStr)){
            serialNumbers = Arrays.asList(serialNumberStr.split(","));
        }
        RealEnergyMonitiorVo realEnergyMonitiorVo = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);

        realEnergyMonitiorVo.setAnnualTotal(realEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
        realEnergyMonitiorVo.setMonthTotal(realEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
        realEnergyMonitiorVo.setTodayTotal(realEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

        Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
        for (CommonStatisVo statisVo : realEnergyMonitiorVo.getCommonStatisVoList()) {
            if(eleMap.get(statisVo.getName()) != null){
                statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()));
            }
        }
        return R.ok(realEnergyMonitiorVo);
    }

    /**
     * 能流监控
     * @param addressId 设备所属地址ID（1：1楼，2：2楼，3：12楼）
     * @param typeId 1:年，2：月，3：日
     * @return
     */
    @GetMapping("/selectEnergyFlowMonitor")
    @ApiOperation("能流监控")
    public R<EnergyFlowMonitorVo> selectEnergyFlowMonitor(@RequestParam(value = "addressId",required = false) Integer addressId
            ,@RequestParam(value = "typeId") Integer typeId){
        String serialNumberStr = "";
        if(addressId == null){
            serialNumberStr = allNumberList;
        }else{
            if(addressId == 1){
                serialNumberStr = oneNumberList;
            }else  if(addressId == 2){
                serialNumberStr = twoNumberList;
            }else{
                serialNumberStr = thrNumberList;
            }
        }
        Date date = DateUtil.offsetHour(new Date(),16);
        List<String> serialNumbers = new ArrayList<>();
        if(StringUtils.isNotEmpty(serialNumberStr)){
            serialNumbers = Arrays.asList(serialNumberStr.split(","));
        }
        RealEnergyMonitiorVo realEnergyMonitiorVo = logService.selectRealEnergyMonitior(typeId,serialNumbers,date);
        RealEnergyMonitiorVo realEnergyMonitiorEleVo = logService.selectRealEnergyMonitiorEle(typeId,serialNumbers,date);

        realEnergyMonitiorVo.setAnnualTotal(realEnergyMonitiorVo.getAnnualTotal().add(realEnergyMonitiorEleVo.getAnnualTotal()));
        realEnergyMonitiorVo.setMonthTotal(realEnergyMonitiorVo.getMonthTotal().add(realEnergyMonitiorEleVo.getMonthTotal()));
        realEnergyMonitiorVo.setTodayTotal(realEnergyMonitiorVo.getTodayTotal().add(realEnergyMonitiorEleVo.getTodayTotal()));

        Map<String,List<CommonStatisVo>> eleMap = realEnergyMonitiorEleVo.getCommonStatisVoList().stream().collect(Collectors.groupingBy(CommonStatisVo::getName));
        for (CommonStatisVo statisVo : realEnergyMonitiorVo.getCommonStatisVoList()) {
            if(eleMap.get(statisVo.getName()) != null){
                statisVo.setValue(statisVo.getValue().add(eleMap.get(statisVo.getName()).get(0).getValue()));
            }
        }
        EnergyFlowMonitorVo energyFlowMonitorVo = new EnergyFlowMonitorVo();
        energyFlowMonitorVo.setCommonStatisVoList(realEnergyMonitiorVo.getCommonStatisVoList());
        energyFlowMonitorVo.setElectricTotal(typeId == 1 ? realEnergyMonitiorVo.getAnnualTotal() : (typeId == 2 ? realEnergyMonitiorVo.getMonthTotal() : realEnergyMonitiorVo.getTodayTotal()));
        energyFlowMonitorVo.setEnergyTotal(energyFlowMonitorVo.getElectricTotal().divide(new BigDecimal("8.167"),2,RoundingMode.HALF_UP));
        energyFlowMonitorVo.setCarbonTotal(energyFlowMonitorVo.getElectricTotal().multiply(new BigDecimal("0.785")).setScale(2, RoundingMode.HALF_UP));
        return R.ok(energyFlowMonitorVo);
    }

}
